<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>


    <script>
        window._on = window.addEventListener,

        window._off = window.removeEventListener;

        window._emit = (type, data) => window.dispatchEvent(new CustomEvent(type, { detail: data }));;

        window._once = (type, listener) => window.addEventListener(type, listener, { once: true, capture: true });
    </script>


    <script>
        function onEventX(ev) {
            console.log("event-x 收到数据:", ev.detail);
        }

        // 订阅
        window._on("event-x", onEventX);
        window._once("event-once", ev => console.log("event-once 收到数据:", ev.detail));

        // once
        window._emit("event-once", { uid: -100, message: "you love me" });
        window._emit("event-once", { uid: -100, message: "you love me" });
        // 订阅和取消订阅
        window._emit("event-x", { uid: 100, message: "i love you" })
        window._off("event-x", onEventX);
        window._emit("event-x", { uid: 100, message: "i love you" })

    </script>


</body>

</html>